function [labels, shapemes] = shapemeQuantize(shapeContexts, k, ...
	shapemes)
% Input:
%   shapeContexts: nImg-by-1 cell array, 
%     each of which is an nSampling-by-nBin array;
%   k: the number of labels;
%   shapemes: shape context centroids for shapemes quantization,
%     if not given, it will be evaluated from `shapeContexts`;
% Output:
%   labels: nImg-by-1 array

	nImg = size(shapeContexts, 1);
	nImgSamples = min(ceil(nImg / 10), 700);

	if ~exist('shapemes', 'var')
		idx = sort(randsample(nImg, nImgSamples));
		scSampled = vertcat(shapeContexts{idx});
		[~, shapemes] = kmeanspp(scSampled, k);
	end

	% k-NN classification
	knnObj = ExhaustiveSearcher(shapemes);
	labels = zeros(nImg, k);
	for i = 1:nImg
		nearestIdx = knnsearch(knnObj, shapeContexts{i});
		labels(i, :) = histc(nearestIdx', 1:k);
	end
